package priv.bruce.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import priv.bruce.entry.MovieInfo;
import priv.bruce.service.MovieInfoService;

public class ModifyMovieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private MovieInfoService movieInfoService = new MovieInfoService();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		int id = Integer.valueOf(request.getParameter("movie_id"));
		String field = request.getParameter("field");
		String value = request.getParameter("modify");
		try {
			List<MovieInfo> findById = movieInfoService.findById(id);
			if (findById.size() == 1) {
				int modifyMovie = movieInfoService.modifyMovie(field, value, id);
				System.out.println("影响行数："+modifyMovie);
				response.getWriter().append(JSON.toJSONString(modifyMovie));
			}else {
				response.getWriter().append(JSON.toJSONString(-1));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		int id = Integer.valueOf(request.getParameter("movie_id"));
		String field = request.getParameter("field");
		String value = request.getParameter("modify");
		try {
			if (field.equals("movie_lang")) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(value);
				List<MovieInfo> findById = movieInfoService.findById(id);
				if (findById.size() == 1) {
					int modifyMovie = movieInfoService.modifyMovie(field, date, id);
					System.out.println("影响行数："+modifyMovie);
					response.getWriter().append(JSON.toJSONString(modifyMovie));
				}else {
					response.getWriter().append(JSON.toJSONString(-1));
				}
			}else if (field.equals("jishu")) {
				List<MovieInfo> findById = movieInfoService.findById(id);
				int jishu = Integer.valueOf(value);
				if (findById.size() == 1) {
					int modifyMovie = movieInfoService.modifyMovie(field, jishu, id);
					System.out.println("影响行数："+modifyMovie);
					response.getWriter().append(JSON.toJSONString(modifyMovie));
				}else {
					response.getWriter().append(JSON.toJSONString(-1));
				}
			}else {
				List<MovieInfo> findById = movieInfoService.findById(id);
				if (findById.size() == 1) {
					int modifyMovie = movieInfoService.modifyMovie(field, value, id);
					System.out.println("影响行数："+modifyMovie);
					response.getWriter().append(JSON.toJSONString(modifyMovie));
				}else {
					response.getWriter().append(JSON.toJSONString(-1));
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
